package com.action.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.base.app_core.View.xrecyclerview.RecyclerAdapter;
import com.base.app_core.View.xrecyclerview.RecyclerViewHolder;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.XYBAUtils;
import com.xinglai.yun.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.iwf.photopicker.PhotoPagerActivity;
import me.iwf.photopicker.PhotoPreview;

/**
 * Created by jiansheng.li on 2017/10/11.
 */

public class ShowImageAdapter extends RecyclerAdapter<String, ShowImageAdapter.ViewHolder> {

    public static final int REQUEST_CODE = 666;

    public boolean isShowDeleteButton = true;
    private final int width;
    private onDefaultImageAddLisener l;

    public ShowImageAdapter() {
        width = (XYBAUtils.getScreenWidth() - XYBAUtils.dip2px(16 * 2) - XYBAUtils.dip2px(8 * 2)) / 3;
    }

    @Override
    protected void fillData(final String item, final ViewHolder holder) {

        ViewGroup.LayoutParams layoutParams = holder.img_content.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = width;
        holder.img_content.setLayoutParams(layoutParams);

        if (item.equals(AppConstants.Const.ADDIMAGETAG)) {
            holder.img_content.setImageResource(R.mipmap.add_member);
            int pd = XYBAUtils.dip2px(24);
            holder.img_content.setPadding(pd, pd, pd, pd);
            holder.img_content.setBackgroundResource(R.drawable.shape_gray_rec);
        } else {
            int pd = XYBAUtils.dip2px(0);
            holder.img_content.setPadding(pd, pd, pd, pd);
            XYBAUtils.displayImageLocal(holder.img_content.getContext(), item, holder.img_content);
            holder.img_content.setBackgroundResource(R.drawable.shape_gray_transparent);
        }

        holder.img_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.equals(AppConstants.Const.ADDIMAGETAG)) {
                    if (l != null) {
                        l.onClickAction(false, item);
                    }
                } else {
                    if (l != null) {
                        l.onClickAction(true, item);
                    }
                }
            }
        });
    }

    public void previewImage(Activity activity, String item) {
        List<String> list = getList();
        if (XYBAUtils.isEmpty(list)) {
            return;
        }
        Intent intent = new Intent(activity, PhotoPagerActivity.class);
        intent.putExtra(PhotoPreview.EXTRA_CURRENT_ITEM, list.indexOf(item));
        intent.putExtra(PhotoPreview.EXTRA_SHOW_DELETE, isShowDeleteButton);
        intent.putStringArrayListExtra(PhotoPreview.EXTRA_PHOTOS, transArraylist(list));
        activity.startActivityForResult(intent, REQUEST_CODE);
    }

    public void setShowDeleteButton(boolean showDeleteButton) {
        isShowDeleteButton = showDeleteButton;
    }

    private ArrayList<String> transArraylist(List<String> list) {
        ArrayList<String> temp = new ArrayList<>();
        temp.addAll(list);
        if (temp.size() == AppConstants.PACKAGE_IMAGE_LENGHT) {
            temp.remove(temp.size() - 1);
        }
        return temp;
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_image_layout;
    }

    public void setOnDefaultImageAddLisener(onDefaultImageAddLisener l) {
        this.l = l;
    }

    public interface onDefaultImageAddLisener {
        void onClickAction(boolean isNoEmpty, String item);
    }

    public class ViewHolder extends RecyclerViewHolder {

        @BindView(R.id.img_content)
        ImageView img_content;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
